# -*- coding: utf-8 -*-

# Copyright (C) 2016 Swift Navigation Inc.
#
# Contact: Pasi Miettinen <pasi.miettinen@exafore.com>
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

"""PR ranging code is a sequence of maximum length of shift register with a
period 1 millisecond and bit rate 511 kbps. PR ranging code is sampled at
the output of 7th stage of the 9-stage shift register. The initialization
vector to generate this sequence is (111111111). The first character of the
PR ranging code is the first character in the group 111111100, and it is
repeated every 1 millisecond. The generating polynomial, which corresponds
to the 9-stage shift register is G(X) = 1 + X^5 + X^9

Function outputs the bitstream as str and time advances from left to right
output[0] ... output[510]
0ms ----------> 1ms

In the sequence '0' is represented as '1', 1 as -1"""

import numpy as np


def generate_glo_ca_code():
  """
  Function generates PRN sequence for Glonass.
  All GLONASS satellites use the same C/A-code,
  generated by a 9-bit shift register.
  """
  code = '111111111'
  output = np.zeros(511, np.int8)
  for i in xrange(len(output)):
    if '0' == code[6]:
      output[i] = 1
    else:
      output[i] = -1
    if int(code[4]) ^ int(code[8]):
      code = '1' + code[:8]
    else:
      code = '0' + code[:8]
  return output

GLOCode = generate_glo_ca_code()
